package user;

import java.sql.ResultSet;
import java.sql.SQLException;


public class LoginValidation {
	User user;
	//private PreparedStatemen pstmt;
	
	public LoginValidation(User user) {
		this.user = user;
	}
	
	/*
	public LoginValidation(String user) {
		this.user = new User();
		this.user.setName("user");
	}
	*/
	
	public boolean isValidate() {
		String pw = getTruePassword(user);
		System.out.println(pw + "??");
		System.out.println(user.getPassword());
		return user.getPassword().equals(pw);
	}
	
	public String getTruePassword(User user) {		
		ResultSet result;
		try {
			result = getPassword(user);
			if(result.next()) {
				System.out.println("Inside if-clause");
				return result.getString(1);
			} else
				return null;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return null;
	}
	
	/** 
     * Initialize database connection
     */
    private ResultSet getPassword(User user) {
    	DBConnector db = DBConnector.getDBConnector();
    	db.prepareStatement("select cus_passcode from customer where customer.cus_login_name = ?");
    	System.out.println(user.getName());
    	db.bindParameter(user.getName());
    	return db.queryPstmt();
    }
    
    public static String getUserId(String username) {
    	DBConnector db = DBConnector.getDBConnector();
    	db.prepareStatement("select cus_ID from customer where customer.cus_login_name = ?");
    	db.bindParameter(username);	
    	try {
    		ResultSet result = db.queryPstmt();
			if(result.next()) {
				return result.getString(1);
			} else
				return null;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return null;
    }
    
}
